/**
 * @(#) AddressProxy.as
 *
 * @date		2009. 02. 18.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 주소변경 - Proxy
 * 				
 */
package com.skt.common_hr.myprofile.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.myprofile_stc.model.vo.PersonalVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class PersonalProxy extends CommonProxy
	{
		public static const NAME:String = "PersonalProxy";
		
		public static const LOAD_PERSONAL_COMPLETE:String 	= "loadPersonalComplete";
		public static const LOAD_PERSONAL_FAILED:String 	= "loadPersonalFailed";
		public static const SAVE_PERSONAL_COMPLETE:String 	= "savePersonalComplete";
		public static const SAVE_PERSONAL_FAILED:String 	= "savePersonalFailed";

		public var personal:PersonalVO = new PersonalVO;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function PersonalProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		
		
		public function load(pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_ACTIO"] = "DIS";
						
			loadData("Z_HR_0124", params, resultLoad, faultLoad); 
		}
		
		/**
		 * 주소를 저장함
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param   pernr: 사번
		 * 			PSTLZ: 우편번호
		 * 			LOCAT: 상세주소
		 * 			SUBTY: 주소 구분 (1-본적, 2-현주소, 3-주민등록주소)
		 * @return void
		 */
		public function save(pernr:String, vo:Object):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_ACTIO"] = "MOD";

			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(vo);
			tables["T_IN"] = table;
			
			saveData("Z_HR_0124", params, tables, resultSave, faultSave); 
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultLoad(event:ResultEvent):void
		{
			if(retCode==0) {
				personal = parsePersonal(event.result.T_OUT);
				facade.sendNotification(LOAD_PERSONAL_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_PERSONAL_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultLoad(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_PERSONAL_FAILED, this);
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultSave(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(SAVE_PERSONAL_COMPLETE, this);
			}
			else {
				facade.sendNotification(SAVE_PERSONAL_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultSave(event:FaultEvent):void
		{
			facade.sendNotification(SAVE_PERSONAL_FAILED, this);
		}
		
		private function parsePersonal(src:ArrayCollection):PersonalVO
		{
			var vo:PersonalVO = new PersonalVO();
			if(src.length>=1) 
			{
				var item:Object = src.getItemAt(0);
				vo.BEGDA		= DateUtil.stringToDateTime(item.BEGDA);			//시작일
				vo.ENDDA		= DateUtil.stringToDateTime(item.ENDDA);			//종료일
				vo.FAMDT		= DateUtil.stringToDateTime(item.FAMDT);			//결혼기념일
				vo.LNMCH		= item.LNMCH;			//성(한자)
				vo.FNMCH		= item.FNMCH;			//이름(한자)
				vo.NACHN		= item.NACHN;			//성(영어)	
				vo.VORNA		= item.VORNA;			//이름(영어)
				vo.CTALL		= parseFloat(item.CTALL).toString();			//신장
				vo.CWEIGHT		= parseFloat(item.CWEIGHT).toString();			//체중 
				vo.CBLOOD		= item.CBLOOD;			// 혈액형
				vo.CARMY1		= item.CARMY1;			//군별
				vo.CARMY2		= item.CARMY2;			//제대구분
				vo.CGBDAT		= DateUtil.stringToDateTime(item.CGBDAT);			//시작일
				vo.CGUBUN		= item.CGUBUN;
			}
			return vo;
		}
	}
}